package com.unicom.priceboard.util;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.util.LabelValueBean;
import org.apache.struts.util.MessageResources;


/**
 * The class using for getting message resources(Properties file and application file).
 * @author NVSu
 */
public class Resources  {
	
  /**
   * Commons Logging instance.
   */	
	private static Log logger = LogFactory.getLog(Resources.class);
	private static MessageResources resourceApp = MessageResources.getMessageResources("properties.ApplicationResources");
	private static MessageResources resourceMsg = MessageResources.getMessageResources("properties.messages");
	private static MessageResources resourceConf = MessageResources.getMessageResources("properties.Config");
	/**
	 *  Contructor method
	 */
	public Resources() {
	}
	
	/**
	 * Return a value that are specified by key.
	 * (from application resource file)
	 * @param key
	 * @return
	 */
	public static  String getAppMessage(String key) {
		
			String ret = "";
			try {
				ret = StringUtils.trim(resourceApp.getMessage(key));
				if ( StringUtils.isEmpty(ret)) {
					
					ret = "";
					//logger.debug("Not found message key in the properties file::" + key);
				}
			} catch (Exception e) {
				logger.debug("Not found message key in the properties file::" + e.getMessage());
			}
			
			return ret;
	}
	
	
	public static  String getConfigMessage(String key) {
		
		String ret = "";
		try {
			ret = StringUtils.trim(resourceConf.getMessage(key));
			if ( StringUtils.isEmpty(ret)) {
				
				ret = "";
				//logger.debug("Not found message key in the properties file::" + key);
			}
		} catch (Exception e) {
			logger.debug("Not found message key in the properties file::" + e.getMessage());
		}
		
		return ret;
}
	
	/**
	 * Return a value that are specified by key.
	 * (from application resource file)
	 * @param key
	 * @return
	 */
	public static  String getMsgMessage(String key) {
		
			String ret = "";
			try {
				ret = resourceMsg.getMessage(key);
				if ( StringUtils.isEmpty(ret)) {
					
					ret = "";
					//logger.debug("Not found message key in the properties file::" + key);
				}
			} catch (Exception e) {
				logger.debug("Not found message key in the properties file::" + e.getMessage());
			}
			
			return ret;
	}	
	
	/**
	 * Return a value that are specified by key.
	 * (from application resource file)
	 * @param key
	 * @return
	 */
	public static  String getAppCompName(String key) {
	
		String ret = "";
		try {
			String keyAdded = "comp.name.item." + key;
			ret = StringUtils.trim(resourceApp.getMessage(keyAdded));
			if ( StringUtils.isEmpty(ret)) {
				
				ret = "";
				//logger.debug("Not found message key in the properties file::" + keyAdded + "::");
			}
		} catch (Exception e) {
			logger.debug("Not found message key in the properties file::" + e.getMessage());
		}
		
		return StringUtils.strip(ret);
	}
	/**
	 * get config 
	 */
	public static  String getConfigName(String key) {
		
		String ret = "";
		try {
			String keyAdded = "comp.name.item." + key;
			ret = StringUtils.trim(resourceConf.getMessage(keyAdded));
			if ( StringUtils.isEmpty(ret)) {
				
				ret = "";
				//logger.debug("Not found message key in the properties file::" + keyAdded + "::");
			}
		} catch (Exception e) {
			logger.debug("Not found message key in the properties file::" + e.getMessage());
		}
		
		return StringUtils.strip(ret);
	}
	
	/**
	 * 
	 * @param idx
	 * @return
	 */
	public static String getUserCheckBox(int idx){
				
		return getAppMessage("ho.checkbox.user." + String.valueOf(idx));
	}
	
	
	/**
	 * 
	 * @param shortName
	 * @return
	 */
	public static String getShortNameUserChk(String longName){
				
		return getAppMessage("ho.checkbox.user.short.name." + String.valueOf(longName));
	}
	
	
	
	/**
	 * 
	 * @return
	 */
	public static LabelValueBean gettCheckBoxItem(String itemName){
		
		LabelValueBean obj = new LabelValueBean();;
		String shortName = "";
			
		if (false == StringUtils.isEmpty(itemName)) {
					
			shortName = getShortNameUserChk(itemName);
					
				
			if ("".equals(shortName)) {				
				obj.setValue(itemName);
				obj.setLabel(itemName);
				}
				else {
				obj.setValue(itemName);
				obj.setLabel(shortName);
				}
		}
		
		return obj;
		//return (LabelValueBean[])objList.toArray(new LabelValueBean[objList.size()]);
	}
	
}
